package com.qiezi.project.decoration;

import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.qiezi.App;
import com.qiezi.util.FontUtils;


public class RecyclerItemDecoration extends RecyclerView.ItemDecoration {
    private int itemSpace;
    private int itemNum;

    String TAG = "";

    /**
     *
     * @param itemSpace item间隔
     * @param itemNum 每行item的个数
     */
    public RecyclerItemDecoration(int itemSpace, int itemNum) {
        this.itemSpace = FontUtils.dip2px(App.getInstance(),itemSpace);
        this.itemNum = itemNum;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        int index = parent.getChildLayoutPosition(view);
        outRect.bottom = itemSpace;
        if (index < itemNum) outRect.top = itemSpace;

        int column = index % itemNum;
        if (itemNum == 1){
            outRect.left = itemSpace;
            outRect.right = itemSpace;
            return;
        }


        outRect.left = itemSpace - column * itemSpace / itemNum;
        outRect.right = (column + 1) * itemSpace / itemNum;

    }
}

